package com.vdong.trade.trading.common.security;


import com.alibaba.fastjson.JSON;
import com.vdong.trade.trading.common.annotation.PermissionsCheck;
import com.vdong.trade.trading.common.exception.ProcessException;
import org.apache.commons.collections.CollectionUtils;
import org.lamb.framework.common.exception.basic.GlobalException;
import org.lamb.framework.common.util.StringUtil;
import org.lamb.framework.core.security.LambPrincipalFactoryContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.reactive.ForwardedHeaderFilter;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import org.springframework.web.server.WebSession;
import reactor.core.publisher.Mono;

import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class Testsss implements WebFilter {


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        System.err.println("WebFilter 我进来了====================");

        String path = exchange.getRequest().getURI().getPath();

        //判断url是否在对应的list中
        Set<String> strings = getPermissions();
        if (!CollectionUtils.isEmpty(strings)) {
            if (strings.contains(path)) {
                return chain.filter(exchange);
            }
        }

        //TODO redis获取用户缓存权限信息

        String principal = LambPrincipalFactoryContainer.getPrincipal();
        // principal 缓存信息为null,则直接放掉,让登录拦截去校验
        if (StringUtil.hasBlank(principal)) {
            return chain.filter(exchange);
        }

        return chain.filter(exchange);
    }

    protected Set<String> getPermissions() {
        return null;
    }
}
